package com.tc.netty.three;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.http.*;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @Auther: tianchao
 * @Date: 2022/2/14 11:15
 * @Description:
 */
@Slf4j
public class TestHttp {
    public static void main(String[] args) {
        new TestHttp().start();
    }

    void start(){
        NioEventLoopGroup boss = new NioEventLoopGroup();
        NioEventLoopGroup worker = new NioEventLoopGroup();
        try{
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.channel(NioServerSocketChannel.class);
            serverBootstrap.group(boss,worker);
            serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                    ch.pipeline().addLast(new HttpServerCodec());
                    ch.pipeline().addLast(new SimpleChannelInboundHandler<HttpRequest>() {
                        @Override
                        protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws Exception {
                            log.debug(msg.uri());
                            DefaultFullHttpResponse response = new DefaultFullHttpResponse(msg.protocolVersion(), HttpResponseStatus.OK);
                            final byte[] bytes = "<h1>hello world</h1>".getBytes();
                            response.content().writeBytes(bytes);
                            response.headers().setInt(HttpHeaderNames.CONTENT_LENGTH,bytes.length);
                            ctx.writeAndFlush(response);
                        }
                    });
//                    ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
//                        @Override
//                        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//                            log.debug("read msg: {}", msg.getClass());
//                            /**
//                             * 2022-02-14 11:21:37.622 [nioEventLoopGroup-3-1] DEBUG com.tc.netty.three.TestHttp - read msg: class io.netty.handler.codec.http.DefaultHttpRequest
//                             * 2022-02-14 11:21:37.686 [nioEventLoopGroup-3-1] DEBUG com.tc.netty.three.TestHttp - read msg: class io.netty.handler.codec.http.LastHttpContent$1
//                             */
//                        }
//                    });
                }
            });
            final ChannelFuture channelFuture = serverBootstrap.bind(8080).sync();
            channelFuture.channel().closeFuture().sync();
        }catch (InterruptedException e){
            log.error("server error {}", e);
        }finally {
            boss.shutdownGracefully();
            worker.shutdownGracefully();
            log.debug("final 释放");
        }

    }
}
